home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / atre12.zip / LF.ZIP / LFEDITIO.C < prev    next >
C/C++ Source or Header  |  1991-07-05  |  9KB  |  331 lines

  1. /***** editor I/O *****/
  2.  
  3. #include <windows.h>
  4. #include <string.h>
  5. #include "filedlg.h"
  6.  
  7. /* in filedlg.c */
  8. int DoFileOpenDlg (HANDLE, WORD, char *, char *, WORD  , char *, POFSTRUCT);
  9. int DoFileSaveDlg (HANDLE, WORD, char *, char *, WORD *, char *, POFSTRUCT);
  10. int DoOutFileDlg  (HANDLE, WORD, char *, char *, WORD *, char *, POFSTRUCT);
  11. LPSTR lstrrchr (LPSTR, char);
  12.  
  13.  
  14. extern char szAppName[];       /*in lfedit.c*/
  15. extern char szFileSpec[];
  16. extern char szFileNameOut[];
  17. extern char szFileNameIn[];
  18.  
  19. BOOL bUserAbort;
  20. HWND hDlgPrint;
  21.  
  22. #pragma argsused
  23.  
  24. BOOL FAR PASCAL PrintDlgProc(HWND hDlg, WORD message, WORD wParam, LONG lParam)
  25.     {
  26.     switch (message)
  27.         {
  28.         case WM_INITDIALOG:
  29.             EnableMenuItem (GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
  30.             return TRUE;
  31.  
  32.         case WM_COMMAND:
  33.             bUserAbort = TRUE;
  34.             EnableWindow(GetParent(hDlg), TRUE);
  35.             DestroyWindow(hDlg);
  36.             hDlgPrint = 0;
  37.             return TRUE;
  38.         }
  39.     return FALSE;
  40.     }
  41.  
  42. #pragma argsused
  43.  
  44. BOOL FAR PASCAL AbortProc (HDC hPrinterDC, short nCode)
  45.     {
  46.     MSG msg;
  47.  
  48.     while(!bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  49.         {
  50.         if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg))
  51.             {
  52.             TranslateMessage(&msg);
  53.             DispatchMessage(&msg);
  54.             }
  55.         }
  56.     return !bUserAbort;
  57.     }
  58.  
  59. HDC GetPrinterDC (void)
  60.     {
  61.     static char szPrinter[80];
  62.     char *szDevice, *szDriver, *szOutput;
  63.  
  64.     GetProfileString("windows", "device", ",,,", szPrinter, 80);
  65.  
  66. #pragma warn -pia
  67.  
  68.     if (((szDevice = strtok (szPrinter, ",")) &&
  69.          (szDriver = strtok (NULL     , ", "))) &&
  70.         (szOutput = strtok (NULL     , ", ")))
  71.  
  72. #pragma warn .pia
  73.  
  74.             return CreateDC(szDriver, szDevice, szOutput, NULL);
  75.  
  76.     return 0;
  77.     }
  78.  
  79. BOOL PrintFile (HANDLE hInst, HWND hwnd, HWND hwndEdit, char *szFileName)
  80.     {
  81.     BOOL        bError = FALSE;
  82.     char        szMsg[40];
  83.     FARPROC     lpfnAbortProc, lpfnPrintDlgProc;
  84.     HDC         hdcPrn;
  85.     NPSTR       psBuffer;
  86.     short       yChar, nCharsPerLine, nLinesPerPage,
  87.                 nTotalLines, nTotalPages, nPage, nLine, nLineNum = 0;
  88.     int         nEscapeReturn;
  89.     TEXTMETRIC  tm;
  90.  
  91.     if (0 == (nTotalLines = (short) SendMessage(hwndEdit,
  92.                                     EM_GETLINECOUNT, 0 , 0L)))
  93.  
  94.         return FALSE;
  95.  
  96.     if (NULL == (hdcPrn = GetPrinterDC()))
  97.         return TRUE;
  98.  
  99.     GetTextMetrics (hdcPrn, &tm) ;
  100.     yChar = tm.tmHeight  + tm.tmExternalLeading;
  101.  
  102.     nCharsPerLine = GetDeviceCaps (hdcPrn, HORZRES) / tm.tmAveCharWidth;
  103.     nLinesPerPage = GetDeviceCaps (hdcPrn, VERTRES) / yChar;
  104.     nTotalPages   = (nTotalLines + nLinesPerPage - 1) / nLinesPerPage;
  105.  
  106.     psBuffer = (NPSTR) LocalAlloc(LPTR, nCharsPerLine);
  107.  
  108.     EnableWindow(hwnd, FALSE);
  109.  
  110.     bUserAbort = FALSE;
  111.     lpfnPrintDlgProc = MakeProcInstance(PrintDlgProc, hInst);
  112.     hDlgPrint = CreateDialog(hInst, "PrintDlgBox", hwnd, lpfnPrintDlgProc);
  113.     SetDlgItemText (hDlgPrint, IDD_FNAME, szFileName);
  114.  
  115.     lpfnAbortProc = MakeProcInstance (AbortProc, hInst);
  116.     Escape (hdcPrn, SETABORTPROC, 0, (LPSTR)lpfnAbortProc, NULL);
  117.  
  118.     strcat (strcat (strcpy (szMsg, szAppName), " - "), szFileName);
  119.  
  120.     nEscapeReturn = Escape(hdcPrn, STARTDOC, strlen(szMsg), szMsg, NULL);
  121.  
  122.     if (nEscapeReturn < 0)
  123.         bError = TRUE;
  124.  
  125.     else
  126.         {
  127.         for (nPage = 0; nPage < nTotalPages; nPage++)
  128.             {
  129.             for (nLine = 0; nLine < nLinesPerPage &&
  130.                             nLineNum < nTotalLines; nLine++, nLineNum++)
  131.                 {
  132.                 *(short *) psBuffer = nCharsPerLine;
  133.                 TextOut(hdcPrn, 0, yChar * nLine, psBuffer,
  134.                     (short) SendMessage (hwndEdit, EM_GETLINE,
  135.                                 nLineNum, (LONG) (LPSTR) psBuffer));
  136.                 }
  137.  
  138.             nEscapeReturn = Escape(hdcPrn, NEWFRAME, 0, NULL, NULL) ;
  139.  
  140.             if (nEscapeReturn < 0)
  141.                 {
  142.                 bError = TRUE;
  143.                 break;
  144.                 }
  145.  
  146. #pragma warn -rch
  147.  
  148.             if (bUserAbort)
  149.                 break;
  150.  
  151. #pragma warn .rch
  152.             }
  153.         }
  154.  
  155.  
  156.     if (!bError)
  157.         Escape(hdcPrn, ENDDOC, 0, NULL, NULL);
  158.  
  159.     if (!bUserAbort)
  160.         {
  161.         EnableWindow(hwnd, TRUE);
  162.         DestroyWindow(hDlgPrint);
  163.         }
  164.  
  165.     if (bError || bUserAbort)
  166.         {
  167.         strcat(strcpy(szMsg, "Could not print: "), szFileName);
  168.         MessageBox (hwnd, szMsg, szAppName, MB_OK | MB_ICONEXCLAMATION);
  169.         }
  170.  
  171.     LocalFree ((LOCALHANDLE) psBuffer);
  172.     FreeProcInstance (lpfnPrintDlgProc);
  173.     FreeProcInstance (lpfnAbortProc);;
  174.     DeleteDC (hdcPrn);
  175.  
  176.     return bError || bUserAbort;
  177.     }
  178.  
  179.  
  180. long FileLength (HANDLE hFile)
  181.     {
  182.     long    lCurrentPos = _llseek(hFile, 0L, 1);
  183.     long    lFileLength = _llseek(hFile, 0L, 2);
  184.  
  185.     _llseek(hFile, lCurrentPos, 0);
  186.  
  187.     return lFileLength;
  188.     }
  189.  
  190. void OkMessageBox(HWND hwnd, char *szString, char *szFileName)
  191.     {
  192.     char szBuffer [40];
  193.  
  194.     wsprintf (szBuffer, szString, (LPSTR) szFileName);
  195.  
  196.     MessageBox(hwnd, szBuffer, szAppName, MB_OK | MB_ICONEXCLAMATION);
  197.     }
  198.  
  199. BOOL GetOutFile(HANDLE hInst, HWND hwnd)
  200.     {
  201.     char szBuffer[40];
  202.     char szFileName[16];
  203.     WORD wStatus;
  204.     char szOutFileSpec[] = "*.OUT";
  205.     char szOutExt [] = ".OUT";
  206.     OFSTRUCT of;
  207. /*
  208.     lstrcpy(szOutFileSpec, szFileNameIn);
  209.  
  210.     *(lstrrchr(szOutFileSpec, '.')) = '\0';
  211.  
  212.     lstrcat(szOutFileSpec, szOutExt);
  213. */
  214.     if (!DoOutFileDlg (hInst, hwnd, szOutFileSpec, szOutExt,
  215.                         &wStatus, szFileName, &of))
  216.         return FALSE;
  217.  
  218.     if (wStatus == 1)
  219.         {
  220.         wsprintf(szBuffer, "Replace existing %s", (LPSTR) szFileName);
  221.         if (IDNO == MessageBox(hwnd, szBuffer, szAppName,
  222.                         MB_YESNO | MB_ICONQUESTION))
  223.  
  224.             return FALSE;
  225.         }
  226.  
  227.     lstrcpy  (szFileNameOut, of.szPathName);
  228.     return TRUE;
  229.     }
  230.  
  231. BOOL ReadFile (HANDLE hInstance, HWND hwnd, HWND hwndEdit, POFSTRUCT pof,
  232.                 char *szFileName, BOOL bAskName)
  233.     {
  234.     DWORD dwLength;
  235.     HANDLE hFile, hTextBuffer;
  236.     LPSTR lpTextBuffer;
  237.  
  238.     if (bAskName)
  239.         {
  240.         if (!DoFileOpenDlg (hInstance, hwnd, szFileSpec, szFileSpec + 1,
  241.                 0x4010, szFileName, pof))
  242.  
  243.             return FALSE;
  244.         }
  245.  
  246. #pragma warn -rng
  247.  
  248.     if (-1 == (hFile = OpenFile(szFileName, pof, OF_READ | OF_REOPEN)))
  249.         {
  250.         OkMessageBox(hwnd, "Cannot open file %s", szFileName);
  251.         return FALSE;
  252.         }
  253.  
  254.     if ((dwLength = FileLength(hFile)) >= 64000)
  255.         {
  256.         _lclose(hFile);
  257.         OkMessageBox(hwnd, "File %s too large", szFileName);
  258.         return FALSE;
  259.         }
  260.  
  261.     if (NULL == (hTextBuffer = GlobalAlloc(GHND, (DWORD) dwLength + 1)))
  262.         {
  263.         _lclose(hFile);
  264.         OkMessageBox(hwnd,"Not enough memory for %s", szFileName);
  265.         return FALSE;
  266.         }
  267.  
  268.     lpTextBuffer = GlobalLock (hTextBuffer);
  269.     _lread(hFile, lpTextBuffer, (WORD) dwLength);
  270.     _lclose(hFile);
  271.  
  272.     lpTextBuffer [(WORD) dwLength] = '\0';
  273.  
  274.     SetWindowText (hwndEdit, lpTextBuffer);
  275.     GlobalUnlock (hTextBuffer);
  276.     GlobalFree(hTextBuffer);
  277.     return TRUE;
  278.     }
  279.  
  280. BOOL WriteFile (HANDLE hInstance, HWND hwnd, HWND hwndEdit, POFSTRUCT pof,
  281.                     char *szFileName, BOOL bAskName)
  282.  
  283.     {
  284.     char szBuffer[40];
  285.     HANDLE hFile, hTextBuffer;
  286.     NPSTR npTextBuffer;
  287.     WORD wStatus, wLength;
  288.  
  289.     if (bAskName)
  290.         {
  291.         if(!DoFileSaveDlg (hInstance, hwnd, szFileSpec, szFileSpec + 1,
  292.                                 &wStatus, szFileName, pof))
  293.  
  294.             return(FALSE);
  295.         
  296.         if (wStatus == 1)
  297.             {
  298.             wsprintf (szBuffer, "Replace existing %s", (LPSTR) szFileName);
  299.             if (IDNO == MessageBox(hwnd, szBuffer, szAppName,
  300.                             MB_YESNO | MB_ICONQUESTION))
  301.                 return FALSE;
  302.             }
  303.         }
  304.  
  305.     else
  306.         OpenFile (szFileName, pof, OF_PARSE);
  307.  
  308.     if (-1 == (hFile = OpenFile(szFileName, pof, OF_CREATE | OF_REOPEN)))
  309.         {
  310.         OkMessageBox(hwnd, "Cannot create file %s", szFileName);
  311.         return FALSE;
  312.         }
  313. #pragma warn .rng
  314.  
  315.     wLength = GetWindowTextLength(hwndEdit);
  316.     hTextBuffer = (HANDLE) SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  317.     npTextBuffer = LocalLock(hTextBuffer);
  318.  
  319.     if (wLength != _lwrite(hFile, npTextBuffer, wLength))
  320.         {
  321.         _lclose(hFile);
  322.         OkMessageBox(hwnd, "Cannot write file %s to disk: ", szFileName);
  323.         return FALSE;
  324.         }
  325.  
  326.     _lclose(hFile);
  327.     LocalUnlock(hTextBuffer);
  328.  
  329.     return TRUE;
  330.     }
  331.